//
//  HomeStessCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import MYModule
import RxSwift
import RxCocoa

class HomeStressCell: HomeBaseCell {
    
    var viewModel: HomeStressViewModel?
    
    let stressNumberLabel = Label()
    
    let stateLabel = Label()
    
    let stateView = StressStateView()
    
    private lazy var measureButton: HomeMeasureButton = {
        let measureButton = HomeMeasureButton()
        measureButton.setTitle("stress_tap_measure_btn_title".language, for: .normal)
        measureButton.setTitle("stress_measure_end_btn_title".language, for: .selected)
        
        let image = UIImage(named: "ic_home_today_stress_measure")
        measureButton.setImage(image, for: .normal)
        
        let disableTitleColor = UIColor.assist_A9A9A9
        let disableImage = image?.originalImageWithColor(color: disableTitleColor)
        
        measureButton.setImage(disableImage, for: .disabled)
        measureButton.setTitleColor(disableTitleColor, for: .disabled)
        
        measureButton.setTitleColor(UIColor.assist_222222, for: .normal)
        measureButton.titleLabel?.font = Fonts.systemFont(ofSize: 14)
        measureButton.backgroundColor = UIColor.assist_F8F8F8
        measureButton.setImageTitleLayout(.imgLeft, spacing: 7)
        measureButton.cornerRadius = 14
        return measureButton
    }()
    
    override func makeUI() {
        super.makeUI()
        
        titleLabel.text = "stress_title".language
        iconImagView.image = UIImage(named: "ic_home_today_stress")
        
        stressNumberLabel.textColor = UIColor.Stress.theme
        stressNumberLabel.font = Fonts.boldSystemFont(ofSize: 28)
        stateLabel.textColor = UIColor.assist_D0D0D0
        stateLabel.font = Fonts.systemFont(ofSize: 10)
        
        measureButton.rx.tap.subscribe(onNext: {[weak self] in
            
//            guard let model = self?.viewModel?.UIReplay.value else { return }
//            self?.measureButton.isSelected = model.isMeasure
            
            BluetoothResponder.shared.willMeasurement {
                if let isSelected = self?.measureButton.isSelected {
                    self?.measureButton.isSelected = !isSelected
                }
                self?.viewModel?.automaticMeasure()
            }
//            self?.reloadData()
        }).disposed(by: measureButton.rx.disposeBag)

        bgView.addSubview(stressNumberLabel)
        bgView.addSubview(stateLabel)
        bgView.addSubview(measureButton)
        bgView.addSubview(stateView)
        
        stressNumberLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(15)
        }
        
        stateLabel.snp.makeConstraints { make in
            make.right.equalTo(stressNumberLabel.snp.right)
            make.centerY.equalTo(dateLabel.snp.centerY)
        }
        
        stateView.snp.makeConstraints { make in
            make.top.equalTo(iconImagView.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }
        
        measureButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(stateView.snp.bottom).offset(20)
            make.height.equalTo(52)
            make.bottom.equalToSuperview().offset(-15)
        }
        
    }
    

    func bindButton(measureState: UIControl.State) {
        
        if measureState == .selected {
            
            measureButton.isEnabled = true
            measureButton.isSelected = true
            
        } else if measureState == .normal {
            
            measureButton.isEnabled = true
            measureButton.isSelected = false
            
        } else if measureState == .disabled {
            
            measureButton.isEnabled = false
            measureButton.isSelected = false
        }
        
    }
    
    override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeStressViewModel else { return }
        
        self.viewModel = viewModel
        
        let disposeBag = DisposeBag()
        
        viewModel.UIReplay.subscribe(onNext: {[weak self] in
            
            self?.bindButton(measureState: $0.measureState)
            
            self?.dateLabel.text = $0.date
            self?.stressNumberLabel.text = $0.stressTitle
            self?.stateLabel.text = $0.state
            self?.stateView.stress = $0.stress
            
            
        }).disposed(by: disposeBag)
        
        viewModel.hasDevicedReplay.subscribe(onNext: {[weak self] in
            
            self?.updateLayouts(hasDevice: $0)
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    func updateLayouts(hasDevice: Bool?) {
        
        let hidden = hasDevice == true ? false : true
        
        measureButton.isHidden = hidden

        if !hidden {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(stateView.snp.bottom).offset(20)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(52)
                make.bottom.equalToSuperview().offset(-15)
                
            }
            
        } else {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(stateView.snp.bottom)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(0)
                make.bottom.equalToSuperview().offset(-15)
                
            }
        }
        
    }
    
}


